﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using Entity = Savico.Library.Entity;
using Business = Savico.Library.Business;


public partial class templates_admin_usercontrols_list_product : System.Web.UI.UserControl
{
    Entity.SanPhamTieuBieu entityProduct = new Savico.Library.Entity.SanPhamTieuBieu();
    Entity.LinhVucKinhDoanh entityBusiness = new Savico.Library.Entity.LinhVucKinhDoanh();
    Business.LinhVucKinhDoanh businessAreaBusiness = new Savico.Library.Business.LinhVucKinhDoanh();
    Business.SanPhamTieuBieu businessProduct = new Savico.Library.Business.SanPhamTieuBieu();
    string ngonngu = "vi";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Alert.Load_DropDownList(ddlLinhVucKD, businessAreaBusiness.SelectVN(), "TenLinhVuc", "IdLinhVuc", "------Lĩnh vực kinh doanh------");
            dtProduct = businessProduct.SelectVN();
            Load_Product();
        }
    }
    protected void btnTiengViet_Click(object sender, EventArgs e)
    {
        ngonngu = "vi";
        Alert.Load_DropDownList(ddlLinhVucKD, businessAreaBusiness.SelectVN(), "TenLinhVuc", "IdLinhVuc", "------Lĩnh vực kinh doanh------");
        dtProduct = businessProduct.SelectVN();
        Load_Product();
        ChuyenDoiNgonNgu();
    }
    protected void btnTiengAnh_Click(object sender, EventArgs e)
    {
        ngonngu = "en";
        Alert.Load_DropDownList(ddlLinhVucKD, businessAreaBusiness.SelectEN(), "TenLinhVuc", "IdLinhVuc", "------Business areas------");
        dtProduct = businessProduct.SelectEN();
        Load_Product();
        ChuyenDoiNgonNgu();
    }
    // Chuyển đổi ngôn ngữ
    private void ChuyenDoiNgonNguTheo_btn()
    {
        try
        {
            foreach (RepeaterItem ListItem in rptDanhSachSanPham.Items)
            {
                Label lbNoiDung = (Label)ListItem.FindControl("lbNoiDung");
                Label lbTieuDe = (Label)ListItem.FindControl("lbTieuDe");
                HyperLink hplSua = (HyperLink)ListItem.FindControl("hplSua");
                LinkButton lbXoa = (LinkButton)ListItem.FindControl("lbXoa");
                if (btnThem.Text == "Thêm")
                {
                    lbNoiDung.Text = "Mô tả sản phẩm : ";
                    lbTieuDe.Text = "Tiêu đề sản phẩm : ";
                    hplSua.Text = "Sửa";
                    lbXoa.Text = "Xóa";
                }
                if (btnThem.Text == "Add")
                {
                    lbNoiDung.Text = "Description : ";
                    lbTieuDe.Text = "Title : ";
                    hplSua.Text = "Repair";
                    lbXoa.Text = "Delete";
                }
            }
            if (btnThem.Text == "Thêm")
            {
                lbTitle.Text = "Danh sách sản phẩm";
                btnLamMoi.Text = "Làm mới";
                btnThem.Text = "Thêm";
                btnTim.Text = "Tìm";
                lbtnFirst.Text = "Đầu";
                lbtnLast.Text = "Cuối";
                lbtnNext.Text = "Tiếp";
                lbtnPrevious.Text = "Trước";
            }
            if (btnThem.Text == "Add")
            {
                lbTitle.Text = "List products";
                btnLamMoi.Text = "Refresh";
                btnThem.Text = "Add";
                btnTim.Text = "Search";
                lbtnFirst.Text = "First";
                lbtnLast.Text = "Last";
                lbtnNext.Text = "Next";
                lbtnPrevious.Text = "Previous";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    private void ChuyenDoiNgonNgu()
    {
        try
        {
            foreach (RepeaterItem ListItem in rptDanhSachSanPham.Items)
            {
                Label lbNoiDung = (Label)ListItem.FindControl("lbNoiDung");
                Label lbTieuDe = (Label)ListItem.FindControl("lbTieuDe");
                HyperLink hplSua = (HyperLink)ListItem.FindControl("hplSua");
                LinkButton lbXoa = (LinkButton)ListItem.FindControl("lbXoa");
                if (ngonngu=="vi")
                {
                    lbNoiDung.Text = "Mô tả sản phẩm : ";
                    lbTieuDe.Text = "Tiêu đề sản phẩm : ";
                    hplSua.Text = "Sửa";
                    lbXoa.Text = "Xóa";
                }
                if (ngonngu=="en")
                {
                    lbNoiDung.Text = "Description : ";
                    lbTieuDe.Text = "Title : ";
                    hplSua.Text = "Repair";
                    lbXoa.Text = "Delete";
                }
            }
            if (ngonngu == "vi")
            {
                lbTitle.Text = "Danh sách sản phẩm";
                btnLamMoi.Text = "Làm mới";
                btnThem.Text = "Thêm";
                btnTim.Text = "Tìm";
                lbtnFirst.Text = "Đầu";
                lbtnLast.Text = "Cuối";
                lbtnNext.Text = "Tiếp";
                lbtnPrevious.Text = "Trước";
            }
            if (ngonngu == "en")
            {
                lbTitle.Text = "List products";
                btnLamMoi.Text = "Refresh";
                btnThem.Text = "Add";
                btnTim.Text = "Search";
                lbtnFirst.Text = "First";
                lbtnLast.Text = "Last";
                lbtnNext.Text = "Next";
                lbtnPrevious.Text = "Previous";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/admin/AddProducts.aspx?action=add");
    }
    protected void btnLamMoi_Click(object sender, EventArgs e)
    {
        if (btnThem.Text=="Thêm")
        {
            dtProduct = businessProduct.SelectVN();
            Load_Product();
            Alert.Load_DropDownList(ddlLinhVucKD, businessAreaBusiness.SelectVN(), "TenLinhVuc", "IdLinhVuc", "------Lĩnh vực kinh doanh------");
            ChuyenDoiNgonNguTheo_btn();
        }
        if (btnThem.Text == "Add")
        {
            dtProduct = businessProduct.SelectEN();
            Load_Product();
            Alert.Load_DropDownList(ddlLinhVucKD, businessAreaBusiness.SelectEN(), "TenLinhVuc", "IdLinhVuc", "------Business areas------");
            ChuyenDoiNgonNguTheo_btn();
        }
    }
    protected void btnTim_Click(object sender, EventArgs e)
    {
        if (btnThem.Text == "Add")
        {
            if (ddlLinhVucKD.SelectedValue != "0")
            {
                entityProduct.IdLinhVuc = Convert.ToInt32(ddlLinhVucKD.SelectedValue);
                dtProduct = businessProduct.SelectSearchEN(entityProduct);
                Load_Product();
            }
            else
            {
                Alert.Show("You haven't been choosed business scope !!!");
            }
        }
        if (btnThem.Text == "Thêm")
        {
            if (ddlLinhVucKD.SelectedValue != "0")
            {
                entityProduct.IdLinhVuc = Convert.ToInt32(ddlLinhVucKD.SelectedValue);
                dtProduct = businessProduct.SelectSearchVN(entityProduct);
                Load_Product();
            }
            else
            {
                Alert.Show("Bạn chưa chọn lĩnh vực sản phẩm cần tìm !!!");
            }
        }
    }
    protected void rptDanhSachSanPham_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "xoa")
            {
                bool delete = false;
                int id = Convert.ToInt32(e.CommandArgument.ToString());
                entityProduct.IdSanPham = id;
                DataTable dtProductKey = businessProduct.SelectPrimarykey(entityProduct);
                File.Delete(Server.MapPath("~/uploads/image-products/" + dtProductKey.Rows[0]["HinhAnh"]));
                delete = businessProduct.Delete(entityProduct);
                if (delete == true)
                {
                    Alert.Show("Xóa sản phẩm thành công!");
                    if (btnThem.Text == "Thêm")
                    {
                        dtProduct = businessProduct.SelectVN();
                        Load_Product();
                        ChuyenDoiNgonNguTheo_btn();
                    }
                    if (btnThem.Text == "Add")
                    {
                        dtProduct = businessProduct.SelectEN();
                        Load_Product();
                        ChuyenDoiNgonNguTheo_btn();
                    }
                }
                else
                {
                    Alert.Show("Xóa sản phẩm không thành công!");
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    // Load sản phẩm
    private void Load_Product()
    {
        if (dtProduct.Rows.Count == 0)
        {
            BindItemsList(dtProduct);
            PhanTrang.Visible = false;
            msgNoRows.Style.Add("display", "block");
        }
        else if (dtProduct.Rows.Count > 0 && dtProduct.Rows.Count <= 3)
        {
            BindItemsList(dtProduct);
            PhanTrang.Visible = false;
            msgNoRows.Style.Add("display", "none");
        }
        else if (dtProduct.Rows.Count > 3)
        {
            BindItemsList(dtProduct);
            PhanTrang.Visible = true;
            msgNoRows.Style.Add("display", "none");
        }
    }

    #region Phân trang bằng PagedDataSource
    private static DataTable dtProduct = new DataTable();
    //Khái báo các property trang, chỉ số đầu, chỉ số cuối
    private int CurrentPage
    {
        get
        {
            object objPage = ViewState["_CurrentPage"];

            int _CurrentPage = 0;
            if (objPage == null)
            {
                _CurrentPage = 0;
            }
            else
            {
                _CurrentPage = (int)objPage;
            }
            return _CurrentPage;
        }
        set { ViewState["_CurrentPage"] = value; }
    }
    private int fistIndex
    {
        get
        {

            int _FirstIndex = 0;
            if (ViewState["_FirstIndex"] == null)
            {
                _FirstIndex = 0;
            }
            else
            {
                _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
            }
            return _FirstIndex;
        }
        set { ViewState["_FirstIndex"] = value; }
    }
    private int lastIndex
    {
        get
        {
            int _LastIndex = 0;
            if (ViewState["_LastIndex"] == null)
            {
                _LastIndex = 0;
            }
            else
            {
                _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
            }
            return _LastIndex;
        }
        set { ViewState["_LastIndex"] = value; }
    }

    PagedDataSource _PageDataSource = new PagedDataSource();

    //Load dữ liệu và phân trang cho dữ liệu
    private void BindItemsList(DataTable dt)
    {
        DataTable dataTable = dt;
        _PageDataSource.DataSource = dataTable.DefaultView;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = 3;
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        this.lblPageInfo.Text = "Trang " + (CurrentPage + 1) + " của " + _PageDataSource.PageCount;
        this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
        this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;

        this.rptDanhSachSanPham.DataSource = _PageDataSource;
        this.rptDanhSachSanPham.DataBind();
        this.doPaging();
    }
    //Phân trang
    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        fistIndex = CurrentPage - 5;
        if (CurrentPage > 5)
        {
            lastIndex = CurrentPage + 5;
        }
        else
        {
            lastIndex = 10;
        }
        if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
        {
            lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
            fistIndex = lastIndex - 10;
        }
        if (fistIndex < 0)
        {
            fistIndex = 0;
        }
        for (int i = fistIndex; i < lastIndex; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }
        this.dlPaging.DataSource = dt;
        this.dlPaging.DataBind();
    }

    //Trang dầu tiên
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 0;
        BindItemsList(dtProduct);
    }
    //Trang sau trang đang hiển thị
    protected void lbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        BindItemsList(dtProduct);
    }
    //Trang tiếp theo trang đang hiển thị
    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        BindItemsList(dtProduct);
    }
    //Trang cuối
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
        BindItemsList(dtProduct);
    }
    //Hàm sự kiện dlPaging
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt32(e.CommandArgument.ToString());
            BindItemsList(dtProduct);
        }
    }
    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            //lnkbtnPage.Style.Add("fone-size", "14px");
            //lnkbtnPage.Font.Bold = true;
            lnkbtnPage.CssClass = "active";
        }
    }
    #endregion
   
}
